package com.icss.daos;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.icss.beans.TeacherTeamVO;
import com.icss.utils.DB;

/**
 * @author 廖鹏
 * @version 创建时间：2010-3-15 下午04:34:04
 * 类说明 师资团队DAO类，管理教师
 */
public class TeacherTeamDao {

	private Dao dao=null;
	private ResultSet rSet=null;
	
	public TeacherTeamDao(){
	}
	
	/**
	 * @author 廖鹏
	 * 功能：添加一个教师信息
	 * @param teacherTeamVO 传入的教师的信息的实体
	 * @return 成功返回true，否则返回false
	 */
	public boolean addTeacher(TeacherTeamVO teacherTeamVO){
		dao=new Dao();
		StringBuffer sql=new StringBuffer();
		sql.append("insert into teacher(teacherName,position,introduction,visiable) values('")
			.append(teacherTeamVO.getTeacherName()).append("','").append(teacherTeamVO.getPosition())
			.append("','").append(teacherTeamVO.getIntroduction())
			.append("',").append(teacherTeamVO.getVisiable()).append(")");
		return dao.execute(sql.toString());
	}
	
	/**
	 * @author 廖鹏
	 * 功能：删除一条教师的记录
	 * @param teacherName 传入的教师的ID
	 * @return 成功返回true，否则返回false
	 */
	public boolean delTeacherTeam(int teacherID){
		dao=new Dao();
		StringBuffer sql=new StringBuffer();
		sql.append("delete from teacher where teacherID=").append(teacherID);
		return dao.execute(sql.toString());
	}
	
	/**
	 * @author 廖鹏
	 * 功能：更新一个教师信息
	 * @param teacherTeamVO 传入的教师的信息的实体
	 * @return 成功返回true，否则返回false
	 */
	public boolean updateTeacherTeam(TeacherTeamVO teacherTeamVO){
		dao=new Dao();
		StringBuffer sql=new StringBuffer();
		sql.append("update teacher set teacherName='").append(teacherTeamVO.getTeacherName())
			.append("',position='").append(teacherTeamVO.getPosition()).append("',introduction='")
			.append(teacherTeamVO.getIntroduction()).append("',visiable=").append(teacherTeamVO.getVisiable())
			.append(" where teacherID=").append(teacherTeamVO.getTeacherID());
		return dao.execute(sql.toString());
	}
	
	/**
	 * 功能：设置老师是否公开
	 * @param teacherID
	 * @param visiable
	 * @return
	 */
	public boolean updateTeacherTeam(int teacherID,int visiable){
		dao=new Dao();
		StringBuffer sql=new StringBuffer();
		sql.append("update teacher set visiable='").append(visiable)
			.append("' where teacherID=").append(teacherID);
		return dao.execute(sql.toString());
	}
	
	/**
	 * @author 廖鹏
	 * 功能：获取一条教师的记录
	 * @param teacherName 传入的教师的名字
	 * @return 成功返回教师实体，否则返回null
	 */
	public TeacherTeamVO getTeacherTeamVO(int teacherID){
		dao=new Dao();
		StringBuffer sql=new StringBuffer();
		sql.append("select * from teacher where teacherID=").append(teacherID);
		rSet=dao.query(sql.toString());
		TeacherTeamVO teacherTeamVO=null;
		try {
			while(rSet.next()){
				teacherTeamVO=new TeacherTeamVO();
				teacherTeamVO.setTeacherID(rSet.getInt("teacherID"));
				teacherTeamVO.setTeacherName(rSet.getString("teacherName"));
				teacherTeamVO.setPosition(rSet.getString("position"));
				teacherTeamVO.setIntroduction(rSet.getString("introduction"));
				teacherTeamVO.setImage(rSet.getString("image"));
				teacherTeamVO.setVisiable(rSet.getInt("visiable"));				
			}			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return teacherTeamVO;
	}
	/**
	 * 功能： 获取记录总数
	 * @return
	 */
	public int getTotalRecord(){
		String sql="select count(*) as totalRecord from teacher";
		dao=new Dao();
		rSet=dao.query(sql);
		int totalRecord=0;
		try {
			while(rSet.next()){
				totalRecord=rSet.getInt("totalRecord");	
			}
		} catch (SQLException e) {
		}
		return totalRecord;
	}
	
	/**
	 * 功能：获取前五个老师
	 * @return
	 */
	public List<TeacherTeamVO> getPageTeacherTeams(){
		dao=new Dao();
		rSet=dao.query("select * from teacher limit 0,5");
		try {
			TeacherTeamVO teacherTeamVO=null;			
			List<TeacherTeamVO> teacherList=new ArrayList<TeacherTeamVO>();
			while(rSet.next()){
				teacherTeamVO=new TeacherTeamVO();
				teacherTeamVO.setTeacherID(rSet.getInt("teacherID"));
				teacherTeamVO.setTeacherName(rSet.getString("teacherName"));
				teacherTeamVO.setPosition(rSet.getString("position"));
				teacherTeamVO.setIntroduction(rSet.getString("introduction"));
				teacherTeamVO.setImage(rSet.getString("image"));
				teacherTeamVO.setVisiable(rSet.getInt("visiable"));
				teacherList.add(teacherTeamVO);
			}
			return teacherList;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			dao.close();
		}
		return null;
	}
	
	/**
	 * 功能：教师分页管理
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List getPageTeacherTeams(int currentPage,int pageSize){
		Connection connection=DB.getConnection();
		List list=new ArrayList();
		try {
			CallableStatement caStatement=connection.prepareCall("{call proc_pageable(?,?,?,?,?,?,?,?,?)}");
			caStatement.setString(3, "teacher");
			caStatement.setString(4, "teacherID,teacherName,position,introduction,image,visiable");
			caStatement.setString(5, "teacherID");
			caStatement.setString(6, "no");
			caStatement.setInt(7, pageSize);
			caStatement.setInt(8, currentPage);
			caStatement.setInt(9, 0);
			caStatement.registerOutParameter(1, java.sql.Types.INTEGER);
			caStatement.registerOutParameter(2, java.sql.Types.INTEGER);
			ResultSet rSet=caStatement.executeQuery();	
			TeacherTeamVO teacherTeamVO=null;			
			List<TeacherTeamVO> teacherList=new ArrayList<TeacherTeamVO>();
			
			while(rSet.next()){
				teacherTeamVO=new TeacherTeamVO();
				teacherTeamVO.setTeacherID(rSet.getInt("teacherID"));
				teacherTeamVO.setTeacherName(rSet.getString("teacherName"));
				teacherTeamVO.setPosition(rSet.getString("position"));
				teacherTeamVO.setIntroduction(rSet.getString("introduction"));
				teacherTeamVO.setVisiable(rSet.getInt("visiable"));
				teacherTeamVO.setImage(rSet.getString("image"));
				teacherList.add(teacherTeamVO);
			}
			list.add(0,teacherList);
			list.add(1,caStatement.getInt(1));
			list.add(2,caStatement.getInt(2));
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally{
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
}
